//
// Created by zhengbo on 23-4-18.
//

#include <GL/gl.h>

#include "EgoTrajectory.h"

EgoTrajectory::EgoTrajectory(std::vector<Point> &points) {
    SetBuffer(points);
    InitBuffer();
}

EgoTrajectory::~EgoTrajectory() {
    ReleaseBuffer();
}

void EgoTrajectory::Update() {
    glBindBuffer(GL_ARRAY_BUFFER, mVbo);
    glBufferData(GL_ARRAY_BUFFER, mPoints.size() * sizeof(Eigen::Vector3f), mPoints.data(), GL_STATIC_DRAW);
}

void EgoTrajectory::Render() {
    glColor3f(1.0, 0.0, 0.0);
    glBindBuffer(GL_ARRAY_BUFFER, mVbo);
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_LINE_STRIP, 0, mPointSize);
    glDisableClientState(GL_VERTEX_ARRAY);
}

void EgoTrajectory::SetBuffer(std::vector<Point> &points) {
    this->mPoints = points;
}

void EgoTrajectory::InitBuffer() {
    if (mVbo == 0) {
        glGenBuffers(1, &mVbo);
    }
}

void EgoTrajectory::ReleaseBuffer() {
    if (mVbo != 0) {
        glDeleteBuffers(1, &mVbo);
        mVbo = 0;
    }
}
